<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item label="员工管理">
          <el-input v-model="filters.username" placeholder="请输入员工名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getEmployee">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <!--		:data :绑定的数据-->
    <!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
    <!--		prop:对应的是:data绑定的对象的属性-->
    <!--		label :描述-->

    <el-table :data="emp" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="100">
        </el-table-column>
        <el-table-column prop="id" label="主键ID" width="100">
        </el-table-column>
      <el-table-column prop="username" label="员工姓名" width="130" sortable>
      </el-table-column>
      <el-table-column prop="email" label="员工邮箱" width="200" sortable>
      </el-table-column>
      <el-table-column prop="state" label="员工状态" width="140" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="password" label="密码" width="140"  sortable>
      </el-table-column>
      <el-table-column prop="phone" label="员工手机号" width="150" sortable>
      </el-table-column>
      <el-table-column prop="shopId" label="店铺id" width="150" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="操作" :visible.sync="addFormVisible"  :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="员工姓名" prop="name">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工邮箱" prop="sn">
          <el-input v-model="addForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="员工状态" prop="state">
          <el-switch
              v-model="addForm.state"
              active-text="启用"
              inactive-text="禁用">
          </el-switch>
        </el-form-item>
        <el-form-item label="密码" prop="sn">
          <el-input v-model="addForm.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="sn">
          <el-input v-model="addForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺id" prop="sn">
          <el-input v-model="addForm.shopId" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

export default {
  data() {
    return {

      filters: {
        username: '',

      },
      emps:[], //员工下拉框数据
      emp: [],
      total: 0,
      page: 1,
      Managers:[], //首页下拉框经理姓名
      listLoading: false,
      sels: [],//列表选中列
      empList:[], //所有员工
      empTree:[], //联级
      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //新增界面数据
      addForm: {
        name: '',
        sn: '',
        state:'',
        username:'',
        managerIds:[],
        parentId:[]
      }

    }
  },
  methods: {
    //性别显示转换
    //row 当前行row = user  column当前列
    formatSex: function (row, column) {
      return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
    },
    handleCurrentChange(val) {
      //var 就是当前页的数据
      this.page = val;
      this.getEmployee();
    },
    //获取员工列表
    getEmployee() {
      //封装的数据
      let para = {
        //page 就是当前页
        page: this.page,
        //这个是高级查询的条件
        keyword: this.filters.username,


      };
      console.log(this.parentId)
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/emp/findPage ",para).then((res) => {
        // console.log("后端发送过来的分页数据:"+JSON.stringify(res));
        //
        console.log(res)
        this.total = res.data.total;
        //
        this.emp = res.data.list;
        //关闭进度条
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //获取上级员工列表
    findemps() {
      //封装的数据
      this.listLoading = true;
      //NProgress.start();
      this.$http.get("/emp").then((res) => {
        console.log(res)
        this.emps = res.data.data
        //关闭进度条
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //获取员工管理员
    findManagers() {
      //封装的数据
      this.listLoading = true;
      //NProgress.start();
      this.$http.get("/emp/findManagers").then((res) => {
        console.log(res)
        this.Managers = res.data.data
        //关闭进度条
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //新增表单 获取所有的员工人员
    findManager() {
      //封装的数据
      this.listLoading = true;
      this.$http.get("/emp").then((res) => {
        console.log(res)
        this.empList = res.data.data
        //关闭进度条
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //新增表单 获取联级tree
    getTree() {
      //封装的数据
      this.listLoading = true;
      this.$http.get("/emp/tree").then((res) => {
        console.log(res)
        this.empTree = res.data.data
        //关闭进度条
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //删除
    handleDel: function (row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.delete("/emp/"+row.id).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getEmployee();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (row) {
      // alert(row)
      //显示编辑页面的模态框
      this.addFormVisible = true;
      //Object.assign 是拷贝对象 BeanUtil.copyof()
      //拷贝对象
      this.addForm = Object.assign({},row)
      //回显状态
      this.addForm.state = !!row.state
      if (row.dirPath){
        var arrs = row.dirPath.split("/");
        console.log(arrs)
        let path =  arrs.slice(1,arrs.length-1);
        // alert(path)
        this.addForm.managerIds = path.map(Number);
        console.log(this.addForm.managerIds)
      }
      /*
     this.addForm.state = row.state ? true:false
      this.addForm = row; //模态框数据回显
*/
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        name: '',
        sn: '',
        state: true,
      };
    },
    //新增 / 修改
    addSubmit: function () {
      //$refs调用js 原生dom元素
      //validate
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //这里才是我们处理数据的地方
            this.addLoading = true;
            //Object.assign 复制对象的
            this.addForm.state =  this.addForm.state ? 1:0
            let para = Object.assign({}, this.addForm);
            this.$http.post("/emp",para).then((res) => {
              this.addLoading = false;
              //elementUi的提示弹框
              this.$message({
                message: res.data.message,
                type: res.data.success?"success":"error"
              });
              //重新set数据
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getEmployee();
            });
          });
        }
      });
    },
    //复选勾选框的方法
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        this.$http.post("emp/batchRemove",para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getEmployee();
        });
      }).catch(() => {

      });
    }
  },
  //就是页面已加载完成就会去触发这个mounted
  mounted() {
    //展示全部数据
    this.getEmployee();
    //下拉框数据展示上级员工
    this.findemps();
    //获取员工管理员下拉框
    this.findManager();
    this.findManagers();
    //员工联级  获取
    this.getTree();
  }
}

</script>

<style scoped>

</style>
